package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.entity.BrandCategory;
import cn.tedu.store.commons.pojo.product.vo.BrandCategoryStandardVO;
import cn.tedu.store.commons.pojo.product.vo.BrandStandardVO;
import cn.tedu.store.commons.pojo.product.vo.CategoryStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.BrandCategoryMapper;
import cn.tedu.store.product.webapi.mapper.BrandMapper;
import cn.tedu.store.product.webapi.mapper.CategoryMapper;
import cn.tedu.store.product.service.IBrandCategoryService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 品牌和分类关系业务实现类
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {

    @Autowired
    private BrandCategoryMapper brandCategoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public void bind(Long brandId, Long categoryId) {
        log.debug("开始处理[绑定品牌和分类]业务 参数:品牌id={},分类id={}", brandId, categoryId);
        //检查品牌是否存在
        BrandStandardVO brandResult = brandMapper.getStandardById(brandId);
        if(brandResult == null){
            String message = "绑定失败,品牌不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //检查分类是否存在
        CategoryStandardVO categoryResult = categoryMapper.getStandardById(categoryId);
        if(categoryResult == null){
            String message = "绑定失败,分类不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //检查是否已经存在绑定关系
        int count = brandCategoryMapper.countByBrandAndCategory(brandId, categoryId);
        if(count != 0){
            String message = "绑定失败,此品牌于此分类的绑定关系已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //执行绑定
        BrandCategory brandCategory = new BrandCategory();
        brandCategory.setBrandId(brandId);
        brandCategory.setCategoryId(categoryId);
        int rows = brandCategoryMapper.insert(brandCategory);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void unbind(Long id) {
        log.debug("开始处理[解绑品牌和分类]业务 参数:{}",id);
        BrandCategoryStandardVO queryResult = brandCategoryMapper.getStandardById(id);
        if(queryResult == null){
            String message = "解除绑定失败,此关系不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //执行解绑
        int rows = brandCategoryMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }
}
